package com.ihg.hiex.admin.web.controller.session.during;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.session.SessionDuringService;
import com.ihg.hiex.admin.service.session.SessionParticipatorService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.service.session.SessionTaskService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.during.SessionCalibration;
import com.ihg.hiex.entity.session.during.SessionCalibrationAnchorScore;
import com.ihg.hiex.entity.session.during.SessionCalibrationComment;
import com.ihg.hiex.entity.session.during.SessionCalibrationCompetency;
import com.ihg.hiex.entity.session.during.SessionCalibrationRecommendation;
import com.ihg.hiex.entity.session.participator.SessionParticipator;
import com.ihg.hiex.entity.session.participator.Type;
import com.ihg.hiex.entity.session.task.SessionTask;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}/report-check")
@MenuSetting("menu-sessions")
public class ReportCheckController {
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private SessionParticipatorService sessionParticipatorService;
    
    @Autowired
    private SessionDuringService sessionDuringService;
    
    @Autowired
    private SessionTaskService sessionTaskService;
    
    private final static Logger logger = LoggerFactory.getLogger(ReportCheckController.class);
    
    /**
     * 
     * @param login
     * @param sessionId
     * @return
     */
    @SuppressWarnings("unchecked")
    @MenuSetting(value = "submenu-session-during-day-two")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView reportCheck(@Secure Login login, @PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/day-two/day-two-report-quality");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        // list assessor subgroup's delegates
        List<SessionParticipator> sessionParticipators = sessionParticipatorService.list(session);
        List<Long> delegateIds = CollectionUtils.extractPropertyList(sessionParticipators, "delegateId");
        
        // list sessionCalibration by delegates
        List<SessionCalibration> sessionCalibrations = sessionDuringService.listSessionCalibration(session, delegateIds);
        modelAndView.addObject("sessionCalibrations", sessionCalibrations);
        
        // list sessionCalibrationCompetencies
        Map<String, Map<String, SessionCalibrationCompetency>> sessionCalibrationCompetencies = sessionDuringService.listSessionCalibrationCompetency(sessionCalibrations);
        modelAndView.addObject("sessionCalibrationCompetencies", sessionCalibrationCompetencies);
        
        // load sessionCalibrationComment
        Map<String, SessionCalibrationComment> sessionCalibrationComments = sessionDuringService.listSessionCalibrationComment(sessionCalibrations);
        modelAndView.addObject("sessionCalibrationComments", sessionCalibrationComments);
        
        // load sessionCalibrationRecommendation
        Map<String, SessionCalibrationRecommendation> sessionCalibrationRecommendations = sessionDuringService.listSessionCalibrationRecommendation(sessionCalibrations);
        modelAndView.addObject("sessionCalibrationRecommendations", sessionCalibrationRecommendations);
        
        SessionTask sessionTask = sessionTaskService.load(session, Type.FACILITATOR, "Report Quality Check");
        modelAndView.addObject("sessionTask", sessionTask);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param calibrationId
     * @param subgroup
     * @return
     */
    @MenuSetting(value = "submenu-session-during-day-two")
    @RequestMapping(path = "/{calibrationId}/check", method = RequestMethod.GET)
    public ModelAndView reportWriting(@Secure Login login,
                                      @PathVariable("sessionId") Long sessionId,
                                      @PathVariable("calibrationId") Long calibrationId,
                                      @RequestParam(name = "subgroup", required = false) String subgroup) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/day-two/day-two-report-check");
        
        SessionCalibration sessionCalibration = sessionDuringService.loadSessionCalibration(calibrationId);
        modelAndView.addObject("sessionCalibration", sessionCalibration);
        
        List<SessionCalibrationCompetency> competencies = sessionDuringService.listSessionCalibrationCompetency(sessionCalibration);
        Map<String, SessionCalibrationCompetency> competenciesMap = CollectionUtils.constructMapValue(competencies, "name");
        modelAndView.addObject("competencies", competencies);
        modelAndView.addObject("competenciesMap", competenciesMap);
        
        SessionCalibrationComment comment = sessionDuringService.loadSessionCalibrationComment(sessionCalibration);
        modelAndView.addObject("comment", comment);
        
        SessionCalibrationRecommendation recommendation = sessionDuringService.loadSessionCalibrationRecommendation(sessionCalibration);
        modelAndView.addObject("recommendation", recommendation);
        
        SessionTask sessionTask = sessionTaskService.load(sessionCalibration.getSession(), Type.FACILITATOR, "Report Quality Check");
        modelAndView.addObject("sessionTask", sessionTask);
        
        // load sessionCalibrationCompetency
        List<SessionCalibrationCompetency> sessionCalibrationCompetencies = sessionDuringService.listSessionCalibrationCompetency(sessionCalibration);
        modelAndView.addObject("sessionCalibrationCompetencies", CollectionUtils.constructMapValue(sessionCalibrationCompetencies, "name"));
        
        // list strength and development needs anchorScore
        Map<String, Map<String, List<SessionCalibrationAnchorScore>>> anchorScores = sessionDuringService.listSessionCalibrationAnchorScore(sessionCalibrationCompetencies);
        modelAndView.addObject("anchorScores", anchorScores);
        
        modelAndView.addObject("subgroup", subgroup);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/submit" , method = RequestMethod.GET)
    public ModelAndView submit(@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/day-two/day-two-report-check-submit");
        
        modelAndView.addObject("sessionId", sessionId);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/submit" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onSubmit(@PathVariable("sessionId") Long sessionId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Session session = sessionService.load(sessionId);
            SessionTask sessionTask = sessionTaskService.load(session, Type.FACILITATOR, "Report Quality Check");
            sessionTaskService.finish(sessionTask);
            
            httpResult.success().message("Successed");
            
        } catch (Exception e) {
            httpResult.fail("500").message("Failed");
            logger.error("#onFinish() - ",e);
        }
        
        return httpResult;
        
    }

}
